//var $ = require('../../libs/conf.js');
var city = require('../../libs/city.js');
const app = getApp()
import { HTTP } from '../../utils/https.js'

let http = new HTTP()
Page({
  data: {
    //城市下拉
    city: '',
    cityData: {},
    hotCityData: [],
    _py: ["hot", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"]
  },
  onLoad: function () {
    var that = this;
    http.request({
      url: 'mini/v1/index/position',
      success: function (res) {
        that.setData({
          city: res.data.name
        })
      }
    })

    var that = this;
    // myCity = wx.getStorageSync("myCity");
    this.setData({
      historyList: wx.getStorageSync("historyList").length > 0 ? wx.getStorageSync("historyList") : []
    });

    this.setData({
      cityData: city.all,
      hotCityData: city.hot
    });
  },
  //选择城市
  selectCity: function (e) {
    var dataset = e.currentTarget.dataset;
    this.setData({
      city: dataset.fullname,
      cityListShow: false,
      inputListShow: false,
      historyListShow: true,
    });
  
  //城市名
   wx.removeStorage({
     key: 'checkcity'
   });
  
    wx.setStorage({
      key: 'checkcity',
      data: dataset,
      success: function(res) {
        wx.switchTab({
          url: '../../pages/index/index'
        })
      }
    })

    
  },
  touchstart: function (e) {
    this.setData({
      index: e.currentTarget.dataset.index,
      Mstart: e.changedTouches[0].pageX
    });
  },
  touchmove: function (e) {
    var history = this.data.historyList;
    var move = this.data.Mstart - e.changedTouches[0].pageX;
    history[this.data.index].x = move > 0 ? -move : 0;
    this.setData({
      historyList: history
    });
  },
  touchend: function (e) {
    var history = this.data.historyList;
    var move = this.data.Mstart - e.changedTouches[0].pageX;
    history[this.data.index].x = move > 100 ? -180 : 0;
    this.setData({
      historyList: history
    });
  },
  //获取文字信息
  getPy: function (e) {
    this.setData({
      hidden: false,
      showPy: e.target.id,
    })
  },

  setPy: function (e) {
    console.log(e)
    this.setData({
      hidden: true,
      scrollTopId: this.data.showPy
    })
  },

  //滑动选择城市
  tMove: function (e) {
    var y = e.touches[0].clientY,
      offsettop = e.currentTarget.offsetTop;

    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      var num = parseInt((y - offsettop) / 12);
      this.setData({
        showPy: this.data._py[num]
      })
    };
  },

  //触发全部开始选择
  tStart: function () {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd: function () {
    this.setData({
      hidden: true,
      scrollTopId: this.data.showPy
    })
  },
  onShareAppMessage: function () {
    return util.share()
  }
})